package com.yyfax.pay.common.filter;

import java.io.IOException;

import javax.annotation.Resource;
import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.springframework.web.multipart.MultipartResolver;

import com.yyfax.framework.filter.YYFilter;

public class MutiPartFilter extends YYFilter {

	@Resource
	private MultipartResolver multipartResolver;

	public void setMultipartResolver(MultipartResolver multipartResolver) {
		this.multipartResolver = multipartResolver;
	}

	private ServletRequest getRequest(ServletRequest req) {
		String enctype = req.getContentType();
		if (StringUtils.isNotBlank(enctype) && enctype.contains("multipart/form-data"))
			// 返回 MultipartHttpServletRequest 用于获取 multipart/form-data 方式提交的请求中 上传的参数
			return multipartResolver.resolveMultipart((HttpServletRequest) req);
		else
			return req;
	}

	@Override
	protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException {
		filterChain.doFilter(getRequest(request), response);
	}

}
